<template>
    <div class="container">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px"
                 class="demo-ruleForm">
            <el-form-item label="模板名称" prop="p1">
                <el-input type="text" v-model="ruleForm2.p1" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="总分数" prop="p2">
                <el-input type="number" v-model="ruleForm2.p2" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="限时(分钟)" prop="p3">
                <el-input type="number" v-model="ruleForm2.p3" auto-complete="off"></el-input>
            </el-form-item>

            <el-form-item label="考试时间">
                <el-date-picker
                    v-model="dateTime"
                    type="datetimerange"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
                <!--<el-button @click="getCheckedNodes">获取</el-button>-->
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import pub from '../../utils/pub'

    export default {
        data: function () {
            var v1 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入模板名称(2-20字)'));
                } else if (value < 2 || value.length > 10) {
                    callback(new Error('2-20位'));
                } else {
                    callback();
                }
            };
            var v2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入试卷总分数'));
                }else {
                    callback();
                }
            };
            var v3 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入试卷答题时间'));
                }else {
                    callback();
                }
            };
            return {
                tree: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                arr: [],
                ruleForm2: {
                    p1: '',
                    p2: '',
                    p3: '',
                },
                rules2: {
                    p1: [
                        {required: true, validator: v1, trigger: 'blur'}
                    ],
                    p2: [
                        {required: true, validator: v2, trigger: 'blur'}
                    ],
                    p3: [
                        {required: true, validator: v3, trigger: 'blur'}
                    ],
                },
                dateTime: [],
            }
        },
        components: {
        },
        props: ['uid'],
        methods: {
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 获取角色信息
            getInfo(qus) {
                // console.log(this.uid)
                if(!this.uid) {
                    this.ruleForm2.p1 = '';
                    this.ruleForm2.p2 = '';
                    this.ruleForm2.p3 = '';
                    this.dateTime = [];
                    return;
                }
                this.loadopen();
                this.config.post('/admin/examinationmode/findById', {id: this.uid}).then(res => {
                    this.loadclose();
                    if (res.data.code === '0') {
                        // console.log(res.data, '获取角色信息');
                        this.ruleForm2.p1 = res.data.examinationMode.name;
                        this.ruleForm2.p2 = res.data.examinationMode.total_score;
                        this.ruleForm2.p3 = res.data.examinationMode.answer_time;
                        this.dateTime = res.data.examinationMode.start_time?[res.data.examinationMode.start_time||'', res.data.examinationMode.end_time||'']:[];
                    } else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                });
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let obj = {
                            'examinationMode.id': this.uid,
                            'examinationMode.name': this.ruleForm2.p1,
                            'examinationMode.total_score': this.ruleForm2.p2,
                            'examinationMode.answer_time': this.ruleForm2.p3,
                            'examinationMode.start_time': this.dateTime[0]||'',
                            'examinationMode.end_time': this.dateTime[1]||'',
                        };
                        //console.log(obj);
                        let url = this.uid ? '/admin/examinationmode/update' : '/admin/examinationmode/add';
                        this.loadopen();
                        this.config.post2(url, obj).then(res => {
                            this.loadclose();
                            if (res.data.code === '0') {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted() {
        }
    }
</script>
